package com.fan.shop.businesslogic;

import java.io.IOException;

import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;
import javax.servlet.http.Part;

import org.apache.log4j.Logger;

import com.fan.shop.global.ShopGlobalUtils;
import com.fan.shop.global.ShopGlobalVariables;
import com.fan.shop.process.ProcExec;

public class FileUploadProcess extends ProcExec<Integer> {

	private static Logger logger = Logger.getLogger(FileUploadProcess.class);
	private String fileName;
	private Part part;
	private int uploadType;
	private String fullFilePath;
	private String relativePath;
	
	private String fileContentType;

	public static Logger getLogger() {
		return logger;
	}

	public static void setLogger(Logger logger) {
		FileUploadProcess.logger = logger;
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public Part getPart() {
		return part;
	}

	public void setPart(Part part) {
		this.part = part;
	}

	public int getUploadType() {
		return uploadType;
	}

	public void setUploadType(int uploadType) {
		this.uploadType = uploadType;
	}

	public String getFullFilePath() {
		return fullFilePath;
	}

	private void setFullFilePath(String fullFilePath) {
		this.fullFilePath = fullFilePath;
	}

	public String getRelativePath() {
		return relativePath;
	}

	public void setRelativePath(String relativePath) {
		this.relativePath = relativePath;
	}

	public String getFileContentType() {
		return fileContentType;
	}

	public void setFileContentType(String fileContentType) {
		this.fileContentType = fileContentType;
	}

	@Override
	public Integer processImpl() {
		ServletContext servletContext = (ServletContext) FacesContext
				.getCurrentInstance().getExternalContext().getContext();
		String webContentRoot = servletContext.getRealPath("/");
		String folderPath = "";
		if (uploadType == ShopGlobalVariables.ICON) {

			folderPath = webContentRoot + ShopGlobalVariables.ICON_PATH;
			relativePath = "/" + ShopGlobalVariables.ICON_PATH + fileName
					+ ShopGlobalUtils.getFileSuffix(fileContentType);
		} else if (uploadType == ShopGlobalVariables.IMAGE) {
			folderPath = webContentRoot + ShopGlobalVariables.IMAGE_PATH;
			relativePath = "/" + ShopGlobalVariables.IMAGE_PATH + fileName
					+ ShopGlobalUtils.getFileSuffix(fileContentType);
		}
		else if (uploadType == ShopGlobalVariables.TEMP) {
			folderPath = webContentRoot + ShopGlobalVariables.TEMP_PATH;
			relativePath = "/" + ShopGlobalVariables.TEMP_PATH + fileName
					+ ShopGlobalUtils.getFileSuffix(fileContentType);
		}

		String fullFileName = folderPath + fileName
				+ ShopGlobalUtils.getFileSuffix(fileContentType);

		setFullFilePath(fullFileName);

		try {
			part.write(fullFileName);
		} catch (IOException e) {
			e.printStackTrace();
			setErrorMessage(e.getMessage());
			return ShopGlobalVariables.FAILED;
		}

		return ShopGlobalVariables.SUCCESS;
	}

}
